#include "contour.h"
#include "image.h"
#include "types.h"
#include "contour_extractor.h"
#include "contour_file.h"

Contour::Contour(){
    label=1;
    pixel_lenght=0;
    isclosed=false;
    pixelsize=SIZE_NOT_DEFINED;
    img = NULL;
}

Contour::Contour(Image *image){
    unsigned int xmax,ymax;
    label=1;
    pixel_lenght=0;
    isclosed=false;
    pixelsize=image->getPixelSize();
    ymax=image->getHeight();
    xmax=image->getWidth();
    img = new Binary_Image(ymax,xmax);
}

Contour::Contour(int ymax,int xmax){
    label=1;
    pixel_lenght=0;
    isclosed=false;
    pixelsize=SIZE_NOT_DEFINED;
}

void Contour::addPoint(Position point){
    if(img!=NULL)
        img->setPixel(point,true);
    points.push_back(point);
    pixel_lenght++;
}

void Contour::addPoint(int x, int y){
    Position point;
    if(img!=NULL)
        img->setPixel(x,y,true);
    point.x=x;
    point.y=y;
    points.push_back(point);
    pixel_lenght++;
}

Contour::~Contour(){
    points.clear();
    delete img;
}

Contour* Contour::newFromImage(Grey_Image *image,unsigned int label,int neightbourhood){

    ContourExtractor extractor;
    return extractor.getContour(image,label,neightbourhood);
}

Contour* Contour::newFromImage(Binary_Image *image,int neightbourhood){
    ContourExtractor extractor;
    return extractor.getContour(image,neightbourhood);
}

Contour* Contour::newFromFile(char* filename){
    return ContourFile::loadContour(filename);
}

void Contour::saveInFile(char* filename){
    ContourFile::saveContour(this, filename);
}

bool Contour::isClosed() const{
    return isclosed;
}

void Contour::setClosed(bool b){
    isclosed = b;
}

int Contour::getLabel() const{
    return label;
}

void Contour::setLabel(int l){
    label = l;
}

float Contour::getPixelSize() const{
    return pixelsize;
}

void Contour::setPixelSize(float s){
    pixelsize=s;
}

int Contour::getPixelLenght() const{
    return pixel_lenght;
}

void Contour::setPixelLenght(int l){
    pixel_lenght = l;
}

int Contour::getOrigin() const{
    return origin;
}

void Contour::setOrigin(int o){
    origin = o;
}

void Contour::removelastPoint(){
    Position removed=points.back();
    points.pop_back();
    if(img!=NULL)
        img->setPixel(removed,false);
    pixel_lenght--;
}

Position Contour::getLastPoint(){
    return points.back();
}

